package net.studou.verify;

import java.util.regex.Pattern;

public class CheckFormat implements CheckChain {

	public static final int USERNAME = 1; // a-z，A-Z，_，0-9；

	public static final int PASSWORD = 2; // a-z，A-Z，0-9；

	public static final int OPERATE_CODE = 3; // 0-9；

	public static final int TAOBAO_URL = 4; // http://**.taobao.com

	public static final int QQ = 5; // 0-9
	
	public static final int CODE = 6; // a-z，A-Z，0-9

	private CheckChain nextChain;

	private int type;

	private String message;

	public CheckFormat(int type, CheckChain nextChain, String message) {
		this.nextChain = nextChain;
		this.type = type;
		this.message = message;
	}

	public String check(String content) {
		boolean rtn = true;
		switch (type) {
		case USERNAME:
			rtn = this.checkUserName(content);
			break;
		case PASSWORD:
			rtn = this.checkPassword(content);
			break;
		case OPERATE_CODE:
			rtn = this.checkOpeCode(content);
			break;
		case TAOBAO_URL:
			rtn = this.checkTaoBaoUrl(content);
			break;
		case QQ:
			rtn = this.checkQQ(content);
			break;
		case CODE:
			rtn = this.checkCode(content);
			break;
		default:
			break;
		}

		if (!rtn) {
			return message;
		} else {
			if (nextChain != null) {
				return nextChain.check(content);
			} else {
				return null;
			}
		}
	}

	
	private boolean checkUserName(String content){
		return Pattern.matches("^\\w{6,15}$", content);
	}
	
	private boolean checkPassword(String content){
		return Pattern.matches("^[a-zA-Z0-9]{6,15}$", content);
	}
	
	private boolean checkOpeCode(String content){
		return Pattern.matches("^\\d{6}$", content);
	}
	
	private boolean checkTaoBaoUrl(String content){
		boolean r = Pattern.matches("^http:\\/\\/.+\\.taobao\\.com$", content);
		return r;
	}
	
	private boolean checkQQ(String content){
		return Pattern.matches("^\\d{5,15}$", content);
	}
	
	private boolean checkCode(String content){
		return Pattern.matches("^[a-zA-Z0-9]{5}$", content);
	}
}
